package base;

import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;

import talk.Bean.MyUser;
import talk.Data.Constant;

/**
 * Created by debby on 2018/4/2.
 * 描述:
 */

public abstract class BaseActivity extends AppCompatActivity {
    private SharedPreferences sp_user;
    private SharedPreferences.Editor editor;
    private static final String UserKey = "SP_USER";
    private static final String UserIDKey = "SP_USERID";
    private static final String UserToken = "SP_TOKEN";
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitleBar(true);
        initProgress();
        sp_user = getSharedPreferences("SP_WeTalk", MODE_PRIVATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        initTitleBar(false);
    }

    protected void showToast(String toast) {
        Toast.makeText(this, toast, Toast.LENGTH_SHORT).show();
    }

    private ImmersionBar mImmersionBar;

    private void initTitleBar(Boolean isInit) {
        if (isInit) {
            mImmersionBar = ImmersionBar.
                    with(this)
                    .statusBarDarkFont(true);
            mImmersionBar.init();   //所有子类都将继承这些相同的属性
        } else {
            if (mImmersionBar != null)
                mImmersionBar.destroy();
        }
    }


    //保存user
    protected void setUser(MyUser user) {
        editor = sp_user.edit();
        String userString = new Gson().toJson(user);
        editor.putString(UserKey, userString);
        editor.apply();
    }

    //保存userID
    protected void setUserID(String user) {
        editor = sp_user.edit();
        editor.putString(UserIDKey, user);
        editor.apply();
    }

    //保存Token
    protected void saveToken(String token) {
        editor = sp_user.edit();
        editor.putString(UserToken, token);
        editor.apply();
    }

    //获取Token
    protected String getToken(String token) {
        return sp_user.getString(UserToken, "");
    }


    //获取user
    public MyUser getUser() {
        String userString = sp_user.getString(UserKey, "");
        return JSON.parseObject(userString, MyUser.class);
    }

    //获取userID
    protected String getUserID() {
        return sp_user.getString(UserIDKey, "");
    }


    private void initProgress() {
        progressDialog = new ProgressDialog(this);
    }

    protected void showProgressDialog(String name) {
        progressDialog.setMessage(name);
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.show();
    }

    protected void dismissProgressDialog() {
        progressDialog.dismiss();
    }


}
